package com.burgeon.backend.SHOPBACKEND.rest;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.Classification;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 根据大小分类查询商品信息
 */
public class QueryClassification extends RESTBase {
    private final static String RANGE = "50";//分页数量
    private String ip;
    private String account;
    private String password;

    public QueryClassification(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public HashMap getData(int start, String id, String value) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(start, id, value), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");
        HashMap data = new HashMap();
        data.put("count",count);
        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        ArrayList<Classification> classificationList = new ArrayList<Classification>();
        for (int i = 0; i < jsonArray.length(); i++) {

            Classification classification = new Classification();
            classification.setName(jsonArray.getJSONArray(i).getString(0));
            classification.setValue(jsonArray.getJSONArray(i).getString(1));
            classification.setNarketDate(jsonArray.getJSONArray(i).getString(2));
            classification.setcSupplierId(jsonArray.getJSONArray(i).getString(3));
            classification.setmSizegroupId(jsonArray.getJSONArray(i).getString(4));
            classification.setPricelist(jsonArray.getJSONArray(i).getString(5));
            classification.setmDimOntId(jsonArray.getJSONArray(i).getString(6));
            classification.setmDimTwoId(jsonArray.getJSONArray(i).getString(7));
            classification.setmDimThrId(jsonArray.getJSONArray(i).getString(8));
            classification.setmDimFourId(jsonArray.getJSONArray(i).getString(9));
            classification.setmDimFiveId(jsonArray.getJSONArray(i).getString(10));
            classification.setId(jsonArray.getJSONArray(i).getString(11));
            classification.setPricelist03(jsonArray.getJSONArray(i).getString(12).equals("null")?"":jsonArray.getJSONArray(i).getString(12));
            classificationList.add(classification);
        }
        data.put("goods",classificationList);

        return data;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(int y, String id, String value) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("NAME");//款号
        columns.add("VALUE");// 品名
        columns.add("MARKETDATE");//上市日
        columns.add("'C_SUPPLIER_ID;NAME'");//供应商
        columns.add("'M_SIZEGROUP_ID;NAME'");//组分类
        columns.add("PRICELIST");//成本价
        columns.add("'M_DIM1_ID;ATTRIBNAME'");//品牌
        columns.add("'M_DIM2_ID;ATTRIBNAME'");//年份
        columns.add("'M_DIM3_ID;ATTRIBNAME'");//大类型
        columns.add("'M_DIM4_ID;ATTRIBNAME'");//小品类
        columns.add("'M_DIM5_ID;ATTRIBNAME'");//镜架系列
        columns.add("ID");
        columns.add("YC_PRICELIST");//一口价

        params.put("columns", columns);
        params.put("table", "M_PRODUCT");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);

        HashMap paramss = new HashMap();
        paramss.put("column", "'M_DIM" + id + "_ID;ATTRIBNAME'");
        paramss.put("condition", value);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    /**
     * 执行方法，向ERP传参
     *
     * @param args
     */
    public static void main(String[] args) {
        QueryClassification q = new QueryClassification("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(q.getData(0, "1", "BO"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}